package com.xesj.shiro;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;

import com.alibaba.fastjson.JSONArray;
import com.xesj.model.Permission;
import com.xesj.model.User;
import com.xesj.service.PermissionServiceI;
import com.xesj.service.RoleServiceI;
import com.xesj.service.UserServiceI;
import com.xesj.util.PageData;


public class ShiroRealm extends AuthorizingRealm{

	    @Resource
	    private UserServiceI userService;
	    @Resource
	    private RoleServiceI roleService;
	    @Resource
	    private PermissionServiceI permissionService ;

	    // 为当前登陆成功的用户授予权限和角色，已经登陆成功了
	    @Override
	    protected AuthorizationInfo doGetAuthorizationInfo(
	            PrincipalCollection principals) {
	        String loginName = (String) principals.getPrimaryPrincipal(); //获取用户名
	        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
	        // authorizationInfo.setRoles();
	        //authorizationInfo.setStringPermissions();
	        List<String> permissions = new ArrayList<String>();
	        List<String>  roles = new ArrayList<String>();
	        permissions = userService.getRolesListByName(loginName);
	        roles = userService.getPermissionsListByName(loginName);
	        authorizationInfo.addRoles(roles);
	        authorizationInfo.addStringPermissions(permissions);
	        return authorizationInfo;
	    }

	    // 验证当前登录的用户，获取认证信息
	    @Override
	    protected AuthenticationInfo doGetAuthenticationInfo(
	            AuthenticationToken token) throws AuthenticationException {
	        String loginName = (String) token.getPrincipal(); // 获取用户名
	        User user = userService.getByUsername(loginName);
	        PageData pd = new PageData();
	        pd.put("loginName", loginName);
	        pd.put("parentId", "0");
	        pd.put("continue", "true");
			//List<PageData> allRoleList = roleService.allRoleListByName(pd);
			//List<Permission> permissionList = permissionService.getPermissionsInfoListByName(pd);
	        List<Permission> treePermissionList = null;
	        try {
				treePermissionList = permissionService.getTreePermissionsListByName(pd);
			} catch (Exception e) {
				e.printStackTrace();
			}
			Subject subject = SecurityUtils.getSubject();
			Session session = subject.getSession();
			session.setAttribute("userPermission", treePermissionList);
	        if(user != null) {
	            AuthenticationInfo authcInfo = new SimpleAuthenticationInfo(user.getLoginName(), user.getLoginPassWord(),ByteSource.Util.bytes(user.getLoginName()),this.getName());
	            return authcInfo;
	        } else {
	            return null;
	        }       
	    }

}
